package com.houxm.jbapu;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public abstract class Model {

	protected Map<String, Object> data = new HashMap<String, Object>();
	protected Set<String> updSet = new HashSet<String>();
	
	public Object get(String column) {
		return this.data.get(column);
	}
	
	public String getStr(String attr) {
		Object s = data.get(attr);
		return s != null ? s.toString() : null;
	}
	
	public Integer getInt(String attr) {
		Number n = (Number)data.get(attr);
		return n != null ? n.intValue() : null;
	}
	
	public Long getLong(String attr) {
		Number n = (Number)data.get(attr);
		return n != null ? n.longValue() : null;
	}
	
	public java.math.BigInteger getBigInteger(String attr) {
		return (java.math.BigInteger)data.get(attr);
	}
	
	public java.util.Date getDate(String attr) {
		return (java.util.Date)data.get(attr);
	}
	
	public java.sql.Time getTime(String attr) {
		return (java.sql.Time)data.get(attr);
	}
	
	public java.sql.Timestamp getTimestamp(String attr) {
		return (java.sql.Timestamp)data.get(attr);
	}
	
	public Double getDouble(String attr) {
		Number n = (Number)data.get(attr);
		return n != null ? n.doubleValue() : null;
	}
	
	public Float getFloat(String attr) {
		Number n = (Number)data.get(attr);
		return n != null ? n.floatValue() : null;
	}
	
	public Short getShort(String attr) {
		Number n = (Number)data.get(attr);
		return n != null ? n.shortValue() : null;
	}
	
	public Boolean getBoolean(String attr) {
		return (Boolean)data.get(attr);
	}
	
	public java.math.BigDecimal getBigDecimal(String attr) {
		return (java.math.BigDecimal)data.get(attr);
	}
	
	public byte[] getBytes(String attr) {
		return (byte[])data.get(attr);
	}
	
	public Number getNumber(String attr) {
		return (Number)data.get(attr);
	}
	
	public Object put(String column, Object value) {
		return this.data.put(column, value);
	}
	
	public Model put(Map<String, Object> map) {
		data.putAll(map);
		return this;
	}
	
	public abstract Object set(String column, Object obj);
	
	public Object clear() {
		this.data.clear();
		this.updSet.clear();
		return this;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append('{');
		boolean first = true;
		for (Entry<String, Object> e : data.entrySet()) {
			if (first)
				first = false;
			else
				sb.append(", ");
			
			Object value = e.getValue();
			if (value != null)
				value = value.toString();
			sb.append(e.getKey()).append(':').append(value);
		}
		sb.append('}');
		return sb.toString();
	}
	
	public boolean equals(Object o) {
		if (!(o instanceof Model))
            return false;
		if (o == this)
			return true;
		return this.data.equals(((Model)o).data);
	}
	
	public int hashCode() {
		return (data == null ? 0 : data.hashCode()) ^ (updSet == null ? 0 : updSet.hashCode());
	}
}
